<template>
  <el-dialog
    :append-to-body="true"
    :close-on-click-modal="false"
    :close-on-press-escape="true"
    :title="$t('menu.sort')"
    :visible.sync="visible"
    width="30vw"
  >
    <el-form
      @keyup.enter.native="confirmChangeSort()"
      ref="dataForm">
      <el-form-item>
        <el-input
          placeholder="输入排序"
          ref="sortInput"
          type="number"
          v-model="form.value"
        >
          <!--                                         v-if="$hasPermission('sys:user:save')"-->
          <el-button
            :loading="requesting"
            @click="confirmChangeSort()"
            slot="append"
          >
            <i class="el-icon-check"/>
          </el-button>
        </el-input>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import mixinViewModule from '@/mixins/mike.view-module'

export default {
  name: 'SortEditDialog',
  mixins: [mixinViewModule],
  data () {
    return {
      visible: false,
      requesting: false,
      requestUrl: '',
      form: {
        name: '',
        id: '',
        value: ''
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.$refs['sortInput'].focus()
      })
    },

    confirmChangeSort (model) {
      if (this.requesting) {
        return
      }

      this.requesting = true

      let url = this.requestUrl.replace('{id}', this.form.id)
      url = url.replace('{sort}', this.form.value)

      this.$http
        .post(`${this.mikeNetworkBase}${url}`)
        .then(({ data: res }) => {
          if (res.code !== 200) {
            return this.$message.error(res.data)
          }
          this.$emit('refreshDataList')

          this.visible = false
          this.$message.success(res.data)
        })
        .finally(() => {
          this.requesting = false
        })
    }
  }
}
</script>

<style scoped></style>
